package com.whf.android.jar.util.notify;

import static android.content.Context.NOTIFICATION_SERVICE;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.blankj.utilcode.util.NotificationUtils;
import com.whf.android.jar.app.Latte;

/**
 * 通知栏
 *
 * @author gc
 * @version 1.0.3
 */
public class NotifyUtils {

    @SuppressLint("StaticFieldLeak")
    private static NotifyUtils notify;

    public static NotifyUtils create(Context context) {
        if (notify == null) {
            notify = new NotifyUtils(context);
        }
        return notify;
    }

    private final Context context;
    private final NotificationCompat.Builder build;
    private final NotificationManager manager;
    /*** 应用频道Id唯一值， 长度若太长可能会被截断 */
    String CHANNEL_ID = "gc_li051201";

    /*** 最长40个字符，太长会被截断 */
    String CHANNEL_NAME = "notify_server";


    /**
     * @param context:上下文对象
     */
    public NotifyUtils(@NonNull Context context) {
        this.context = context;
        build = new NotificationCompat.Builder(context, CHANNEL_ID);
        manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        //Android 8.0 以上需包添加渠道
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(CHANNEL_NAME,
                    CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            channel.enableLights(true); //是否在桌面icon右上角展示小红点
            channel.setLightColor(Color.RED); //小红点颜色
            channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
            manager.createNotificationChannel(channel);
        }
        build.setSmallIcon(Latte.getAppIcon());//设置通知小ICON
        build.setLargeIcon(Latte.getAppLarge());
    }

    public NotifyUtils setContent(String contentTitle, String contentText) {
        contentText = contentText.replace("!", ".").replace("！", "。");
        build.setContentTitle(contentTitle);//设置通知栏标题
        build.setContentText(contentText); //设置通知栏显示内容
        build.setWhen(System.currentTimeMillis());//通知产生的时间，会在通知信息里显示，一般是系统获取到的时间
        build.setPriority(Notification.PRIORITY_DEFAULT); //设置该通知优先级
        build.setAutoCancel(true);//设置这个标志当用户单击面板就可以让通知将自动取消
        build.setDefaults(Notification.DEFAULT_ALL);//向通知添加声音、闪灯和振动效果
        return this;
    }

    /**
     * <p>true，设置他为一个正在进行的通知,
     *
     * @param ongoing:通常是用来表示一个后台任务,以某种方式正在等待,如一个文件下载,同步操作
     */
    public NotifyUtils setOngoing(boolean ongoing) {
        build.setOngoing(ongoing);
        return this;
    }

    /**
     * @param intent:设置通知栏点击意图
     */
    public NotifyUtils setIntent(Intent intent) {
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
        return setContentIntent(pendingIntent);
    }

    /**
     * @param intent:设置通知栏点击意图
     */
    public NotifyUtils setContentIntent(PendingIntent intent) {
        build.setContentIntent(intent);
        return this;
    }

    /**
     * @param number:设置通知集合的数量
     */
    public NotifyUtils setNumber(int number) {
        build.setNumber(number);
        return this;
    }

    /**
     * @param ticker:通知首次出现在通知栏，带上升动画效果的
     */
    public NotifyUtils setTicker(String ticker) {
        build.setTicker(ticker);
        return this;
    }

    /**
     * @return get Notification
     */
    public Notification getNotification() {
        return build.build();
    }

    /**
     * show
     *
     * @param id 唯一标识
     */
    public void notify(int id) {
        NotificationUtils.cancel(id);
        manager.notify(id, getNotification());
    }

}
